<?php
namespace Home\Controller;
use Think\Controller;

class UploadController extends Controller
{
    public function refund_upload()
    {
        $new_dir = './Uploads/refund/';
        if(!is_dir($new_dir)){
            mkdir($new_dir ,0777 ,true);
        }
        $upload = new \Think\Upload();
        $upload->exts =  array('jpg', 'png', 'jpeg');
        $upload->rootPath = $new_dir;
        $upload->saveName = substr(md5(time() .rand(100000 ,999999)) ,-10);
        $upload->maxSize = 5242880;
        $upload->replace  = true; // 覆盖同名文件
        $info = $upload->upload();
        if(!$info) {
            $state = "ERROR" .$upload->getError();
        }else{
            $pic_url = $info['file']['savepath']. $info['file']['savename'];
            $path = $new_dir.$pic_url;
            $state = "SUCCESS";
        }
        $return_data['state'] = $state;
        $return_data['url'] = $path;
        $this->ajaxReturn($return_data ,'json');
    }

    public function img_del()
    {
        $file_name = I("post.img_name");
        if(!file_exists($file_name)){
            $this->ajaxReturn(array("status" => "error" ,"msg" => "文件不存在") ,'json');
        }
        @unlink($file_name);
        $this->ajaxReturn(array("status" => "success"),'json');
    }
}